from django.db import models
from django.utils import timezone


class Employee(models.Model):
    class Gender(models.IntegerChoices):  # 枚举类型，限定取值范围
        MAN = 1, '男'
        FEMALE = 2, '女'

    class Meta:
        db_table = 'employees'
        verbose_name = '员工'

    emp_no = models.IntegerField(primary_key=True, verbose_name='工号')
    birth_date = models.DateField(verbose_name='生日')
    first_name = models.CharField(max_length=14, verbose_name='名')
    last_name = models.CharField(max_length=16, verbose_name='姓')
    gender = models.SmallIntegerField(verbose_name='性别', choices=Gender.choices)
    hire_date = models.DateField()
    email = models.EmailField(blank=True, null=True)
    content = models.TextField(blank=True, null=True)
    created = models.DateTimeField(auto_now_add=True)  # 自动设置创建时间 # 添加显式默认值
    # 可选：添加 updated 字段
    updated = models.DateTimeField(auto_now=True)  # 自动更新修改时间

    @property
    def name(self):
        return "{}-{}".format(self.first_name, self.last_name)

    def __str__(self):
        return "<Emp {},{}>".format(self.emp_no, self.name)

    __repr__ = __str__


class Salary(models.Model):
    class Meta:
        db_table = "salaries"

    id = models.AutoField(primary_key=True)  # 额外增加的主键，Django不支持联合主键
    emp_no = models.ForeignKey(Employee, on_delete=models.CASCADE, db_column='emp_no', related_name='salaries')
    from_date = models.DateField()
    salary = models.IntegerField(verbose_name='工资')
    to_date = models.DateField()
